﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

using RTW.Core.Extensions;
using RTW.Core.Helper;
using RTW.Models;
using RTW.Servers;
using RTW.Web.Comm;

namespace RTW.Web.Controllers
{
    public class LoginController :Controller
    {


        DepartmentServer _departmentServer;
        UserServer _userServer;
        public LoginController() {
            _departmentServer = new DepartmentServer();
            _userServer = new UserServer();
        }
        // GET: Login
        public IActionResult Index()
        {
            IList<TDepartment> list = _departmentServer.GetAll();
            List<SelectListItem> listitem = new List<SelectListItem>();
            foreach (var dep in list)
            {
                listitem.Add(new SelectListItem
                {
                    Text = dep.DMName,
                    Value = dep.ID
                });
            }
            ViewBag.database = listitem;
            return View();
        }


        public IActionResult DoLogin()
        {
            Hashtable hash = new Hashtable();
            try
            {
                string UserAccount = Request.Form["UserAccount"] ;
                string UserPwd =  Request.Form["UserPwd"];
                string DMID = Request.Form["DMID"];
                UserPwd = SecurityHelper.Encrypt(UserPwd);
                TUserInfo model = _userServer.Login(UserAccount, UserPwd, DMID); 
                if (model != null)
                {
                    HttpContext.Session.SetObjectAsJson("User", model);
                  //  HttpContext.Session.Set("User",model);
                    //context.Session["User"] = model;
                   // DelTemp(context);//删除临时文件

                   // base.AddLog("登录了系统", "系统操作", "/Login.aspx");
                    hash["result"] = 1;
                    hash["UserName"] = model.UserName;

                    //new BasePage().Logging("登录系统");//日志
                }
                else
                {
                    hash["result"] = 2;
                }
            }
            catch (Exception err)
            {
                hash["errMsg"] = err.Message;
                #region 写错误日志
                string Msg = hash["errMsg"].ToString();
                // bp.AddErrLog(Msg, "Login.aspx");
               // bp.AddErrLog(Msg, bp.UserDMID);
                #endregion
                hash["result"] = 0;
            }
            
            return new JsonResult(hash);
        }



        /// <summary>
        /// 获取下拉框格式数据
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> GetDDL_ListAsync()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            IList<TDepartment> list = await _departmentServer.GetAllAsync();
            sw.Stop();
            TimeSpan ts = sw.Elapsed;
            sw.Start();
            List<SelectListItem> listitem = new List<SelectListItem>();
            foreach (var dep in list)
            {
                listitem.Add(new SelectListItem
                {
                    Text = dep.DMName,
                    Value = dep.ID
                });
            }
            sw.Stop();
            TimeSpan ts3 = sw.Elapsed;
            return Json(listitem);
        }

        /// <summary>
        /// 获取下拉框格式数据
        /// </summary>
        /// <returns></returns>
        public JsonResult GetDDL_ListFromjson()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            IList<TDepartment> list = _departmentServer.GetAll();
            sw.Stop();
            TimeSpan ts = sw.Elapsed;
            sw.Start();
            List<SelectListItem> listitem = new List<SelectListItem>();
            foreach (var dep in list)
            {
                listitem.Add(new SelectListItem
                {
                    Text = dep.DMName,
                    Value = dep.ID
                });
            }
            sw.Stop();
            TimeSpan ts3 = sw.Elapsed;
            return Json(listitem);
        }



        /// <summary>
        /// 获取下拉框格式数据
        /// </summary>
        /// <returns></returns>
        public JsonResult GetDDL_List()
        {
            string listitem = "[{\"disabled\":false,\"group\":null,\"selected\":false,\"text\":\"湖北省人民检察院\",\"value\":\"27137dffa6bb48bc9b7f97dc82964dd4\"}]";
            return Json(listitem);
        }
    }
}